﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using ZLCMS.Data.Accessor;
using NHibernate.Criterion;
using NHibernate;
using ZLCMS.Business;
using ZLCMS.Data.Common;

namespace ZLCMS.Web.Example
{
    public partial class SearchListTemplate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int currentPageIndex = string.IsNullOrEmpty(Request[anp_list.UrlPageIndexName]) ? 1 : int.Parse(Request[anp_list.UrlPageIndexName]);
            anp_list.PageSize = string.IsNullOrEmpty(Request["pagen"]) ? 15 : int.Parse(Request["pagen"]);

            if (string.IsNullOrEmpty(Request["s"]))
            {
                lb_keyword.Text = "请输入关键词";
            }
            else
            {
                BindList(currentPageIndex, anp_list.PageSize);
                lb_keyword.Text = "搜索关键词：" + Request["s"];
            }
        }

        private void BindList(int pageIndex, int pageSize)
        {
            string whereString = string.Format(" from DocInfo p where Title like '%" + Request["s"] + "%'");

            string countString = " select count(*) " + whereString;

            using (ISession iNHSession = SessionFactoryHelper.CreateSession())
            {
                anp_list.RecordCount = Convert.ToInt32(iNHSession.CreateQuery(countString).UniqueResult());

                string dataString = " select p " + whereString + " order by p.TypeId, p.UpdateDate";

                rp_list.DataSource = iNHSession.CreateQuery(dataString).SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize).List();

                rp_list.DataBind();
            }
        }
    }
}
